package com.yangbo;
/*
* 策略模式
* 两种例子:
* ----------- 超市购物的选择 满多少减多少 ,全部打9折优惠
* 1.首先定义 接口 折扣接口     实际金额 --> (打折接口) --> 需付金额
* 2. 满100减的class implement (折扣接口)
*    全体9折的class implement (折扣接口)
* 3.定义一个上下文类Contextclass 需要传入一个(策略的实例对象) ----上下文就是一个洗衣机
* {
*    private DiscountStratey discountstratey
*
*    public void setDiscountStratey (DiscountStratey discountstratey)
*   {
*     this.discountstratey = discountstatey
*
*   }
*
*    public int  ApplyDiscount(int price)
*   {
*      discountstratey.applydiscount()
*   }
* }
*
* 4.Main方法
* {
*      根据用户传的方案号
*      DiscountStrategy discountStratey;
*      switch()
*      {
*        case 1: discountStratey =  new DiscountStratey1();
*        case 2: discountStratey =  new DiscountStratey1();
*      }
*      new Context().set(discountStratey) .ApplyDiscount()...
* }
*
*
*
*
* 2. 铠甲勇士拿瓦的变身器 ,插入驮拏多的,和拿瓦的很不一样
*
*
* */
import java.util.Scanner;
// 抽象购物优惠策略接⼝
interface DiscountStrategy {
    int applyDiscount(int originalPrice);
}
// 九折优惠策略
class DiscountStrategy1 implements DiscountStrategy {
    @Override
    public int applyDiscount(int originalPrice) {
        return (int) Math.round(originalPrice * 0.9);
    }
}
// 满减优惠策略
class DiscountStrategy2 implements DiscountStrategy {
    private int[] thresholds = {100, 150, 200, 300};
    private int[] discounts = {5, 15, 25, 40};
    @Override
    public int applyDiscount(int originalPrice) {
        for (int i = thresholds.length - 1; i >= 0; i--) {
            if (originalPrice >= thresholds[i]) {
                return originalPrice - discounts[i];
            }
        }
        return originalPrice;
    }
}
// 上下⽂类
class DiscountContext {
    private DiscountStrategy discountStrategy;
    public void setDiscountStrategy(DiscountStrategy discountStrategy) {
        this.discountStrategy = discountStrategy;
    }
    public int applyDiscount(int originalPrice) {
        return discountStrategy.applyDiscount(originalPrice);
    }
}
public class PolicyPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 读取需要计算优惠的次数
        int N = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < N; i++) {
            // 读取商品价格和优惠策略
            String[] input = scanner.nextLine().split(" ");
            int M = Integer.parseInt(input[0]);
            int strategyType = Integer.parseInt(input[1]);
            // 根据优惠策略设置相应的打折策略
            DiscountStrategy discountStrategy;
            switch (strategyType) {
                case 1:
                    discountStrategy = new DiscountStrategy1();

                    break;
                case 2:
                    discountStrategy = new DiscountStrategy2();
                    break;
                default:
                    // 处理未知策略类型
                    System.out.println("Unknown strategy type");
                    return;
            }
            // 设置打折策略
            DiscountContext context = new DiscountContext();
            context.setDiscountStrategy(discountStrategy);
            // 应⽤打折策略并输出优惠后的价格
            int discountedPrice = context.applyDiscount(M);
            System.out.println(discountedPrice);
        }
    }
}